

# -*- coding: utf-8 -*-
import pymysql
import os
import shutil

# 连接到MySQL数据库
connection = pymysql.connect(host='127.0.0.1', user='root', password='Gztt@0851', database='gz_erp_db', port=3336)

# 查询pbi_attach表并获取url字段
sql = "SELECT elec_cert FROM pbi_person_code"
with connection.cursor() as cursor:
    cursor.execute(sql)
    results = cursor.fetchall()

# 关闭数据库连接
connection.close()

# 定义源目录和目标目录
source_dir = "/usr/local/file/rds/temp/"
destination_dir = "/usr/local/file/rds/opoc_certificate_imgs/"

# 遍历查询结果，将源目录中的文件复制到目标目录
for row in results:
    elec_cert = row[0]
    if elec_cert:
        file_name = os.path.basename(elec_cert)
        source_file_path = os.path.join(source_dir, file_name)
        destination_file_path = os.path.join(destination_dir, file_name)

        if os.path.isfile(source_file_path):
            shutil.copy2(source_file_path, destination_file_path)

            print("File '{}' copied successfully.".format(file_name))
        else:
            print("File '{}' not found in source directory.".format(file_name))
